let we = require('../../we/index.js')
new class extends we.Page {
    data() {
        return {
            showDialog:false,
            target: '/pages/goods/index'
        }
    }

    onLoad(option) {
        if (option.target) {
            this.setData({
                target: decodeURIComponent(option.target)
            })
        }

        if (wx.getStorageSync("sid")) {
            return this.getConfig()
        }
        this.setData({
            showDialog:true
        })


        /*  this.$login().then(data => {
              this.$getSetting({scope: 'scope.userInfo'}).then(res => {
                  let auth = res.authSetting["scope.userInfo"]
                  this.$getUserInfo().then(userInfo => {
                      this.getSession(code, userInfo)
                  }).catch(err => {
                      if (!auth) {
                          wx.navigateBack({delta: 0})
                      }
                  })
              })
          })*/
    }

    getSession(code, userInfo) {
        this.$post("/user/session", {code: code, userInfo: userInfo}).then(sid => {
            wx.setStorageSync("sid", sid)
            this.$app.userInfo = userInfo
            this.getConfig()
        }).catch(err => {
            wx.showModal({
                title: "错误",
                content: err.message,
                showCancel: false
            })
        })
    }

    getConfig() {
        this.$post("/product/config").then(res => {
            wx.setStorageSync("freight", res.freight)
            wx.reLaunch({url: this.data.target})
        }).catch(err => {
            wx.showModal({
                title: "错误",
                content: err.message,
                showCancel: false
            })
        })
    }

    getInfo(userData) {
        this.setData({
            showDialog:false
        })
        this.$login().then(data => {
            this.getSession(data.code, userData.detail)
        })
    }
}
